package com.bo.sina.send;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.bo.exception.AppException;
public class SendPostMessage {   
  
    private HttpURLConnection conn = null;   
       
        public WeiboResponse send(String urlAddr,String setcode, Map paramsMap,Map<String,String> requestMap) throws Exception {   
           
            String msg = null;   
             StringBuffer params = new StringBuffer();   
  
             Iterator it = paramsMap.entrySet().iterator();   
            while(it.hasNext()){   
                 Entry element = (Entry)it.next();   
                 params.append(element.getKey());   
                 params.append("=");   
                 params.append(element.getValue());   
                 params.append("&");   
             }   
           
            if(params.length() > 0){   
                 params.deleteCharAt(params.length()-1);   
             }   
           
            try{   
                 URL url = new URL(urlAddr);   
                 conn = (HttpURLConnection)url.openConnection();   
  
                 conn.setDoOutput(true);   
                 conn.setRequestMethod("POST");   
                 conn.setUseCaches(false);   
                 conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");   
                 conn.setRequestProperty("Content-Length", String.valueOf(params.length())); 
                 conn.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                 conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
                 conn.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.5");
                 conn.setRequestProperty("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
                 
                 for(Entry<String,String> entry:requestMap.entrySet()){
                	 conn.setRequestProperty(entry.getKey(),entry.getValue());
                 }
                 
//                 conn.setRequestProperty("Referer","http://weibo.com/?c=spr_web_sq_firefox_weibo_t001");
                 conn.setDoInput(true);   
                 conn.connect();   
  
                 OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "GBK");   
                 out.write(params.toString());   
                 out.flush();   
                 out.close();   
               
                int code = conn.getResponseCode();   
                if (code == 200) {   
	       			BufferedReader l_reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),setcode));  
	       			 //得到返回的信息
	       			StringBuffer sTotalString = new StringBuffer("");  
	       			String sCurrentLine=null;  
	       			while ((sCurrentLine = l_reader.readLine()) != null) {  
	       			  sTotalString.append(sCurrentLine); 
	       			  sTotalString.append("\n");
	       			}  
	       			l_reader.close();
//	       			for(String entry:conn.getHeaderFields().get("Set-Cookie")){
//	       				System.out.println(entry);
//	       			}
//	       			;
	       			return  new WeiboResponse(conn.getHeaderFields().get("Set-Cookie"),sTotalString.toString());
                 }else{
                     BufferedReader l_reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),setcode));  
                     //得到返回的信息
                    StringBuffer sTotalString = new StringBuffer("");  
                    String sCurrentLine=null;  
                    while ((sCurrentLine = l_reader.readLine()) != null) {  
                      sTotalString.append(sCurrentLine); 
                      sTotalString.append("\n");
                    }  
                    l_reader.close();
                    throw  new AppException(String.valueOf(code),sTotalString.toString());
//                    return  new WeiboResponse(conn.getHeaderFields().get("Set-Cookie"),);
                 }
             }finally{   
                 conn.disconnect();   
             }   
//            return null;   
     }   
}   
